<?php
/**
 * 充值订单提交接口
 */
namespace Api\Controller;

use Think\Controller;

class NonCardController extends Controller {

    public function _initialize() {
	    $this->m_recharge_non_card = D("Admin/RechargeNonCard");
    }

    public function index() {
    	exit(json_encode(array('x'=>'y')));
    }


    public function detail(){
        $detail_id = I("request.detail_id");

        $noncard_data = $this->m_recharge_non_card->where(array("id"=>$detail_id))->find();
        $noncard_data['total_add_point'] = $noncard_data['general_point'] + $noncard_data['self_point'];

        $sale = M("Sale")->where(array("id"=>$noncard_data['sale_id']))->find();
        $logistics_user = M('users')->where(array('id'=>$noncard_data['uid']))->find();

        $data = array(
            'title' => '无卡收款单',
            'detail_id' => $detail_id,
            'content' =>array(
                array(
                    'name' => '流水号',
                    'value' => $noncard_data['number'],
                ),
                array(
                    'name' => '收款时间',
                    'value' => date("Y-m-d H:i:s",$noncard_data['create_time']),
                ),
                array(
                    'name' => '账单名目',
                    'value' => '无卡收款',
                ),
                array(
                    'name' => '充值金额',
                    'value' => $noncard_data['price'],
                ),
                array(
                    'name' => '充值机构',
                    'value' => $sale['sale_name'].'',
                ),
                array(
                    'name' => '操作员',
                    'value' => $logistics_user['user_nicename'],
                ),
            ),
        );
        exit(json_return('1','',$data));
    }

    public function rechargepost(){
    	$sale_id = I("post.sale_id");
    	$user_id = I("post.user_id");
    	$price = I("post.recharge_price");
    	$desc = I("post.desc");

    	$lid = 1;	// 个人账户的物流公司

    	if(empty($price)){
            exit(json_return('0','充值金额不能为空！'));
    	}

        $admin_card_give_price = get_config_by_key('admin_card_give_price');    //管理员充值赠送金额
        $logistics_card_give_price = get_config_by_key('logistics_card_give_price');    //管理员开发费

        $insert['price'] = round($price,2);
        $insert['general_price'] = round($admin_card_give_price,2);
        $insert['self_price'] =  round($logistics_card_give_price,2);
        $insert['create_time'] = time();
        $insert['model'] = 2;
        $insert['sale_id'] = $sale_id;
        $insert['desc'] = $desc;
        $insert['uid'] = $user_id;
        $insert['number'] = 'N'.date("YmdHis").rand(10000,99999);
        $insert['lid'] = $lid;
        $insert["status"] = 1;
        $result =  $this->m_recharge_non_card->add($insert);

    	if ($result) {
            exit(json_return('1','',$insert));
    	} else {
            exit(json_return('0','收款失败！'));
    	}
    }

    public function rechargeupdate(){
        $recharge_number = I("post.recharge_number");

        $result = $this->m_recharge_non_card->where(array('number'=>$recharge_number))->save(array('status'=>2));
        if ($result) {
            $result = $this->m_recharge_non_card->where(array('number'=>$recharge_number))->find();
            exit(json_return('1','',array('detail_id'=>$result['id'])));
            // $this->success("充值成功！");
        } else {
            exit(json_return('0','回调失败！'));
        }
    }
}

